package com.mandroid.forecast.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.MessageFormat;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class Utils {

	private final static String GEO_PLANET="http://where.yahooapis.com/v1/places.q(''{0}'')?appid={1}";
	
	private final static String WEATHER ="http://weather.yahooapis.com/forecastrss?w={0}&u={1}";
	
	public static Boolean isNullOrEmpty(String str) {
		Boolean nullOrEmpty = false; 
		if (str == null || "".equals(str.trim())) {
			nullOrEmpty = true;
		}
		return nullOrEmpty;
	}
	
	public static String getGeoPlanetUrl(String place) {
		place = place.replaceAll(" ", "%20");
		String locationUrl = MessageFormat.format(GEO_PLANET, place, Constants.GEO_PLANET_APP_ID);
		return locationUrl;
	}
	
	public static String getWeatherUrl(String woeid, String unit) {
		return MessageFormat.format(WEATHER, woeid, unit);
	}
	
	public static InputStream openUrlForInput(String url){
		InputStream is = null;
		HttpClient client = new DefaultHttpClient();
		try {
			URI uri = new URI(url);
			HttpGet get = new HttpGet(uri);
			HttpResponse response = client.execute(get);
			HttpEntity entity = response.getEntity();
			
			is = entity.getContent();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return is;

	}

}
